<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表情</title>
    <style>
        .list {
            display: flex;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="list"></div>
    <button class="prev">上一页</button>
    <button class="next">下一页</button>
    <span></span>
</body>

</html>
<script>
    function ajax(method, url, params) {
        function objToSearchString(obj) {
            let str = ''
            for (const key in obj) {
                str += `${key}=${obj[key]}&`
            }
            return str.slice(0, str.length - 1)
        }
        return new Promise((resolve, reject) => {
            let xhr = new XMLHttpRequest()
            xhr.open(method, method == 'get' ? url + '?' + objToSearchString(params) : url)
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
            xhr.send(method == 'get' ? null : objToSearchString(params))
        })
    }

    let page = 1
    function render(page) {
        ajax('get', 'https://yantianfeng.com/api/emotion', { page: page, count: 10 }).then((res) => {
            document.querySelector('.list').innerHTML = res.emotions.map((item) => {
                return `<div class="item">
                            <img src="${item['url']}" alt="">
                            <p>${item['title']}</p>
                        </div>
                        `
            }).join('')
        })
    }
    document.querySelector('.prev').onclick = () => {
        document.querySelector('.next').disabled = false
        if (page > 1) {
            render(--page)
            if (page == 1) {
                document.querySelector('.prev').disabled = true
            }
        }
        document.querySelector('span').innerText = page + '/' + 4
    }
    document.querySelector('.next').onclick = () => {
        document.querySelector('.prev').disabled = false
        if (page < 4) {
            render(++page)
            if (page == 4) {
                document.querySelector('.next').disabled = true
            }
        }
        document.querySelector('span').innerText = page + '/' + 4
    }
    render(1)
    document.querySelector('.prev').disabled = true
    document.querySelector('span').innerText = page + '/' + 4
</script>